package com.ebocy.hammer.core.service;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.ebocy.hammer.core.db.AutoIncreaseID;
import com.ebocy.hammer.core.db.DaoManager;
import com.ebocy.hammer.core.db.IDao;
import com.ebocy.hammer.core.domain.GamePlayer;
import com.ebocy.hammer.core.domain.GamePlayerItem;
import com.ebocy.hammer.core.domain.Item;
import com.ebocy.hammer.core.log.EbocyLogManager;

/**
 * 玩家物品操作
 *
 * @author mafei
 *
 */
public class ItemService {
	protected static Logger logger = EbocyLogManager.getSystemLogger();
	private static IDao<GamePlayerItem> gamePlayerItemDao = null;
	private static IDao<Item> itemDao = null;
	private static ItemService itemService = null;

	public static ItemService getInstance() {
		if (itemService == null) {
			itemService = new ItemService();
		}
		return itemService;
	}

	@SuppressWarnings("unchecked")
	private ItemService() {
		if (gamePlayerItemDao == null) {
			gamePlayerItemDao = DaoManager.getInstance().getDao(GamePlayerItem.class);
		}
		if (itemDao == null) {
			itemDao = DaoManager.getInstance().getDao(Item.class);
		}
	}

	/**
	 * 检查用户道具
	 *
	 * @param gamePlayer
	 * @param itemId
	 * @return
	 */
	public boolean checkItem(GamePlayer gamePlayer, int itemId) {
		boolean bool = false;
		// 获取用户背包信息
		List<GamePlayerItem> itemList = getKnapsack(gamePlayer.getGamePlayerId());
		// 遍历用户背包物品
		for (GamePlayerItem item : itemList) {
			// 检查物品是否存在
			if (item.getItemId() == itemId) {
				// 检查是否装备
				if (item.getKey() != 0) {
					bool = true;
				}
				break;
			}
		}
		return bool;
	}

	/**
	 * 查找用户背包物品详细
	 *
	 * @param itemId
	 *            物品id
	 * @return
	 */
	public GamePlayerItem findGamePlayerItem(long itemId) {
		return gamePlayerItemDao.findOne(GamePlayerItem.GAMEPLAYERITEMID, itemId);
	}

	/**
	 * 检查用户形象
	 *
	 * @param gamePlayerId
	 *            用户id
	 * @param kind
	 *            形象id
	 * @return
	 */
	public boolean checkRole(long gamePlayerId) {
		boolean sign = false;
		List<GamePlayerItem> itemList = gamePlayerItemDao.findAll();
		for (GamePlayerItem item : itemList) {
			if (item.getKind() == 3) {
				sign = true;
				break;
			}
		}
		return sign;
	}

	/**
	 * 获取用户背包信息
	 *
	 * @param gamePlayerId
	 *            用户游戏id
	 * @return
	 */
	public List<GamePlayerItem> getKnapsack(long gamePlayerId) {
		List<GamePlayerItem> itemList = gamePlayerItemDao.find(GamePlayer.GAMEPLAYERID, gamePlayerId);
		return itemList;
	}

	public void updateItem(GamePlayerItem gamePlayerItem, Map<?, ?> values) {
		gamePlayerItemDao.update(gamePlayerItem.getId(), values);
	}

	/**
	 * 查找道具详细信息
	 */

	public Item findItemById(long itemId){
		return itemDao.findOne("itemId", itemId);
	}

	/**
	 * 给用户增加物品
	 *
	 * @param gamePlayerId
	 *            用户游戏id
	 * @param itemId
	 *            物品静态id
	 * @param key
	 *            装备位置,0为未装备,其他为绑定键值
	 * @param kind
	 *            物品种类,1=锤子,2=道具,3=形象
	 * @param effectiveTime
	 *            过期时间,单位天
	 * @param createTime
	 *            装备时间
	 * @return
	 */
	public GamePlayerItem addItem(long gamePlayerId, int itemId, int key, byte kind, short effectiveTime, long createTime) {
		GamePlayerItem gamePlayerItem = new GamePlayerItem();
		long gamePlayerItemId = AutoIncreaseID.getInstance().getAutoIncreaseID(AutoIncreaseID.GAME_PLAYER_ITEM_ID);
		// 设置物品唯一id
		gamePlayerItem.setGamePlayerItemId(gamePlayerItemId);
		// 设置物品玩家id
		gamePlayerItem.setGamePlayerId(gamePlayerId);
		// 物品id
		gamePlayerItem.setItemId(itemId);
		// 物品是否装备状态,0为未装备,其他为装备位置
		gamePlayerItem.setKey(key);
		// 物品种类
		gamePlayerItem.setKind(kind);
		// 物品期限
		gamePlayerItem.setEffectiveTime(effectiveTime);
		// 装备时间
		gamePlayerItem.setCreateTime(createTime);
		// 保存物品
		gamePlayerItem = (GamePlayerItem) gamePlayerItemDao.insert(gamePlayerItem);
		// 返回物品id
		return gamePlayerItem;
	}
}
